package com.itheima.myreader;


import java.io.*;
import java.util.Arrays;


/**
 * @Author 王加鑫
 * @Date 2021/5/20 10:41
 * @Version 1.0
 */
public class Demo01 {
    public static void main(String[] args) {
        BufferedReader reader = null;
        BufferedWriter writer = null;
        //读取文件里的内容
        try {
            reader = new BufferedReader(new FileReader("day13_字符流/test"));
            String readLine;
            StringBuilder sb = new StringBuilder();
            //获取每一行的数据
            while ((readLine = reader.readLine()) != null){
                //将每一行数据以空格分割字符串
                String[] stuLine = readLine.split(" ");
                //定义一个跟每一行数据长度一样的int数组
                int[] num = new int[stuLine.length];
                for (int i = 0; i < stuLine.length; i++) {
                    //将字符串转化成int类型方便排序
                    num[i] = Integer.parseInt(stuLine[i]);
                }
                //将数组排序
                Arrays.sort(num);
                //将数组塞进一个stringBuilder对象中
                for (int i = 0; i < num.length; i++) {
                    sb.append(num[i] + " ");
                    if(i == num.length-1){
                        sb.append(System.lineSeparator());
                    }
                }
            }
            System.out.println(sb);
            //写入数据
            writer = new BufferedWriter(new FileWriter("day13_字符流/test",true));
            writer.write(sb.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
